#define _CRT_SECURE_NO_WARNINGS	

/*
struct ListNode {
    int val;
    struct ListNode *next;
    ListNode(int x) : val(x), next(NULL) {}
};*/
class Partition {
public:
    ListNode* partition(ListNode* pHead, int x)
    {
        // write code here
        ListNode* tail1, * head1, * tail2, * head2;
        head1 = tail1 = (ListNode*)malloc(sizeof(ListNode));
        head2 = tail2 = (ListNode*)malloc(sizeof(ListNode));
        ListNode* pcur = pHead;
        while (pcur)
        {
            if (pcur->val < x)
            {
                tail1->next = pcur;
                tail1 = tail1->next;
            }
            else
            {
                tail2->next = pcur;
                tail2 = tail2->next;
            }
            pcur = pcur->next;
        }
        tail2->next = NULL;
        tail1->next = head2->next;
        ListNode* ret = head1->next;
        free(head1);
        free(head2);
        return ret;
    }
};